package com.ruoyi.web.controller.wq;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.service.AppConfigService;
import com.ruoyi.framework.web.service.DynamicService;
import com.ruoyi.system.domain.vo.SysConfigVO;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.wq.domain.WqDynamic;
import com.ruoyi.wq.domain.vo.WqDynamicVO;
import com.ruoyi.wq.service.IWqDynamicService;
import com.ruoyi.wx.domain.WxMaForm;
import com.ruoyi.wx.service.WxMaUserService;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;

/**
 * 动态Controller
 * 
 * @author ruoyi
 * @date 2022-03-12
 */
@RestController
@RequestMapping("/wq/dynamic")
public class WqDynamicController extends BaseController {
	
    @Autowired
    private IWqDynamicService wqDynamicService;
    
    @Autowired
    private WxMaUserService wxMaUserService;
    
    @Autowired
    private ISysUserService sysUserService;
    
    @Autowired
    private AppConfigService appConfigService;
    
    @Autowired
    private DynamicService dynamicService;
    
    /**
     * 查询动态列表
     */
    @Anonymous
    @GetMapping("/list")
    public TableDataInfo list(WqDynamicVO wqDynamic){
    	String value = "0";
    	wqDynamic.setIsAuth(null);
    	wqDynamic.setOpenFlag(null);
    	if(StringUtils.isNotBlank(wqDynamic.getAppId())){
    		SysConfigVO config = new SysConfigVO();
        	config.setAppId(wqDynamic.getAppId());
        	config.setUserId(wqDynamic.getUserId());
        	config.setScene(wqDynamic.getScene());
        	value = appConfigService.getChatValue(config);
        	if("0".equals(value) || "2".equals(value)){
        		// 审核期间
        		wqDynamic.setOpenFlag("1");
        	}else{
        		wqDynamic.setIsAuth("0");
        	}
    	}
    	
    	// 默认只看正常的
    	wqDynamic.setStatus("0");
    	if(wqDynamic.getUserId() != null && wqDynamic.getUserId() > 0){
    		SysUser user = sysUserService.getById(wqDynamic.getUserId());
        	if("1".equals(user.getIsBlack())){
        		wqDynamic.setStatus(null);
        	}
    	}
    	
    	wqDynamic.setAppId(null);
    	
        TableDataInfo info = wqDynamicService.getPageList(wqDynamic);
        return info;
    }
    
    /**
     * 查询动态列表
     */
    @Anonymous
    @GetMapping("/playList")
    public TableDataInfo playList(WqDynamicVO wqDynamic){
    	String value = "0";
    	wqDynamic.setIsAuth(null);
    	wqDynamic.setOpenFlag(null);
    	if(StringUtils.isNotBlank(wqDynamic.getAppId())){
    		SysConfigVO config = new SysConfigVO();
        	config.setAppId(wqDynamic.getAppId());
        	config.setUserId(wqDynamic.getUserId());
        	value = appConfigService.getPlayValue(config);
        	if(wqDynamic.getShareUserId() != null){
        		value = "1";
        	}
        	if("0".equals(value) || "2".equals(value)){
        		// 审核期间
        		wqDynamic.setOpenFlag("1");
        	}
    	}
    	
    	// 默认只看正常的
    	wqDynamic.setStatus("0");
    	if(wqDynamic.getUserId() != null && wqDynamic.getUserId() > 0){
    		SysUser user = sysUserService.getById(wqDynamic.getUserId());
        	if("1".equals(user.getIsBlack())){
        		wqDynamic.setStatus(null);
        	}
    	}
    	
    	if("0".equals(value)){
    		wqDynamic.setStatus("2");
    		wqDynamic.setAppId(null);
    	}
    	
        TableDataInfo info = wqDynamicService.getPageList(wqDynamic);
        return info;
    }
    
    /**
     * 查询自己发布的动态列表
     */
    @GetMapping("/myList")
    public TableDataInfo myList(WqDynamic wqDynamic){
        TableDataInfo info = wqDynamicService.getMyPageList(wqDynamic);
        return info;
    }
    
    /**
     * 查询自己发布的动态列表
     */
    @Anonymous
    @GetMapping("/getPageListByUserId")
    public TableDataInfo getPageListByUserId(WqDynamic wqDynamic){
        TableDataInfo info = wqDynamicService.getPageListByUserId(wqDynamic);
        return info;
    }
    
    /**
     * 获取动态详细信息
     */
    @GetMapping(value = "/getDetail")
    public AjaxResult getDetail(WqDynamicVO dynamic){
        return AjaxResult.success(wqDynamicService.getDetail(dynamic));
    }
    
    /**
     * 获取游戏详细信息
     */
    @Anonymous
    @GetMapping(value = "/getGameDetail")
    public AjaxResult getGameDetail(String id){
        return AjaxResult.success(wqDynamicService.getGameDetail(id));
    }
    
    /**
     * 获取ID
     */
    @Anonymous
    @GetMapping(value = "/getUid")
    public AjaxResult getUid(){
        return AjaxResult.success(IdUtil.fastSimpleUUID());
    }
    
    /**
     * 新增动态
     */
    @RepeatSubmit
    @PostMapping("/add")
    public AjaxResult add(@RequestBody WqDynamic wqDynamic){
    	Long userId = SecurityUtils.getUserId();
    	SysUser user = sysUserService.getById(userId);
    	/*if("1".equals(user.getIsBlack())){
    		Date openTime = user.getOpenTime();
    		Date now = new Date();
    		if(openTime == null){
    			openTime = DateUtil.offsetDay(now, 2);
    			user.setOpenTime(openTime);
    			sysUserService.updateById(user);
    		}
    		if(now.compareTo(openTime) < 1){
    			String time = DateUtil.formatDateTime(openTime);
        		throw new ServiceException("解封时间【"+time+"】");
    		}
    	}*/
    	WxMaForm wxMaForm = new WxMaForm();
    	wxMaForm.setAppId(user.getAppId());
    	wxMaForm.setMessage(wqDynamic.getMessage());
    	wxMaForm.setOpenId(user.getOpenId());
    	Boolean checkMessage = wxMaUserService.checkMessage(wxMaForm);
    	if(!checkMessage){
    		R.fail("内容涉嫌违规");
    	}
    	wqDynamic.setUserId(userId);
        return toAjax(dynamicService.saveDynamic(wqDynamic));
    }
    
    /**
     * 新增动态
     */
    @PostMapping("/addPartyDynamic")
    public AjaxResult addPartyDynamic(@RequestBody WqDynamic wqDynamic){
    	Long userId = SecurityUtils.getUserId();
    	SysUser user = sysUserService.getById(userId);
    	WxMaForm wxMaForm = new WxMaForm();
    	wxMaForm.setAppId(user.getAppId());
    	wxMaForm.setMessage(wqDynamic.getMessage());
    	wxMaForm.setOpenId(user.getOpenId());
    	Boolean checkMessage = wxMaUserService.checkMessage(wxMaForm);
    	if(!checkMessage){
    		R.fail("内容涉嫌违规");
    	}
    	wqDynamic.setUserId(userId);
        return toAjax(dynamicService.addPartyDynamic(wqDynamic));
    }
    
    /**
     * 抢红包
     */
    @PostMapping("/addParty")
    public AjaxResult addParty(@RequestBody WqDynamic wqDynamic){
    	Long userId = SecurityUtils.getUserId();
    	wqDynamic.setUserId(userId);
        return toAjax(dynamicService.addParty(wqDynamic));
    }
    
    /**
     * 新增动态
     */
    @RepeatSubmit
    @PostMapping("/addVip")
    public AjaxResult addVip(@RequestBody WqDynamic wqDynamic){
    	Long userId = SecurityUtils.getUserId();
    	SysUser user = sysUserService.getById(userId);
    	if("1".equals(user.getIsBlack())){
    		Date openTime = user.getOpenTime();
    		Date now = new Date();
    		if(openTime == null){
    			openTime = DateUtil.offsetDay(now, 1);
    			user.setOpenTime(openTime);
    			sysUserService.updateById(user);
    		}else{
    			if(now.compareTo(openTime) < 1){
        			String time = DateUtil.formatDateTime(openTime);
            		throw new ServiceException("解封时间【"+time+"】");
        		}
    		}
    	}
    	WxMaForm wxMaForm = new WxMaForm();
    	wxMaForm.setAppId(user.getAppId());
    	wxMaForm.setMessage(wqDynamic.getMessage());
    	wxMaForm.setOpenId(user.getOpenId());
    	Boolean checkMessage = wxMaUserService.checkMessage(wxMaForm);
    	if(!checkMessage){
    		R.fail("内容涉嫌违规");
    	}
    	wqDynamic.setUserId(userId);
    	return AjaxResult.success(dynamicService.saveVipDynamic(wqDynamic));
    }
    
    /**
     * 新增动态
     */
    @RepeatSubmit
    @PostMapping("/addVipDynamic")
    public AjaxResult addVipDynamic(@RequestBody WqDynamic wqDynamic){
    	Long userId = SecurityUtils.getUserId();
    	SysUser user = sysUserService.getById(userId);
    	if("1".equals(user.getIsBlack())){
    		Date openTime = user.getOpenTime();
    		Date now = new Date();
    		if(openTime == null){
    			openTime = DateUtil.offsetDay(now, 1);
    			user.setOpenTime(openTime);
    			sysUserService.updateById(user);
    		}else{
    			if(now.compareTo(openTime) < 1){
        			String time = DateUtil.formatDateTime(openTime);
            		throw new ServiceException("解封时间【"+time+"】");
        		}
    		}
    	}
    	WxMaForm wxMaForm = new WxMaForm();
    	wxMaForm.setAppId(user.getAppId());
    	wxMaForm.setMessage(wqDynamic.getMessage());
    	wxMaForm.setOpenId(user.getOpenId());
    	Boolean checkMessage = wxMaUserService.checkMessage(wxMaForm);
    	if(!checkMessage){
    		R.fail("内容涉嫌违规");
    	}
    	wqDynamic.setUserId(userId);
    	return AjaxResult.success(dynamicService.addVipDynamic(wqDynamic));
    }
    
    /**
     * 删除动态
     */
	@GetMapping("/delete")
    public AjaxResult delete(String id){
        return toAjax(wqDynamicService.removeById(id));
    }
	
	/**
     * 删除动态
     */
	@GetMapping("/pass")
    public AjaxResult pass(String id){
		dynamicService.passById(id);
        return AjaxResult.success();
    }
	
	/**
     * 资料重置
     */
	@GetMapping("/editUser")
    public AjaxResult editUser(String id){
		dynamicService.editUser(id);
        return AjaxResult.success();
    }
	
	/**
     * 删除动态
     */
	@GetMapping("/reject")
    public AjaxResult reject(String id){
		dynamicService.rejectById(id);
        return AjaxResult.success();
    }
	
}
